package admin.controllers;

import helpers.IdUniqueHelper;
import helpers.StringHelper;

import java.io.IOException;
import java.util.Date;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admin.data.DealData;
import admin.data.StaffData;
import admin.data.StoreData;
import admin.data.StoreItemData;
import admin.data.TransactionData;
import admin.forms.StaffForm;

import models.StaffModel;
import models.StoreItemModel;
import models.StoreModel;
import models.TransactionModel;


public class EditStoreItemServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		response.setCharacterEncoding("UTF-8");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String params = path.replaceAll("/admin/store-item/edit/", "");
		
		StoreItemModel storeItemModel = new StoreItemModel();

		if(params.length()>3)
		{
			StoreItemData storeData = new StoreItemData();
			storeItemModel = storeData.getItem(params);
		}
		
		request.setAttribute("storeItemModel", storeItemModel);

		try {
			request.getRequestDispatcher("/admin/pages/edit_store_item.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		response.setCharacterEncoding("UTF-8");
		
		String productId = (String) request.getParameter("productId");
		int amount = 0;
		try { amount = Integer.parseInt(request.getParameter("amount")); } catch (Exception e) { amount = 0; }
		
		if (amount>0) {
			StoreItemData storeItemData = new StoreItemData();
			StoreItemModel storeItemModel = storeItemData.getItem(productId);
			if(storeItemModel.getId().length()>3)
			{
				storeItemModel.setInventory(storeItemModel.getInventory() - amount);
				storeItemData.insert(storeItemModel);
				
				HttpSession session = request.getSession();
				session.setAttribute("success", "Xuất hàng thành công !");
				
				TransactionModel transaction = new TransactionModel();
				transaction.setTransactionId(IdUniqueHelper.getId());
				transaction.setDealId(storeItemModel.getDealId());
				transaction.setStoreItemId(storeItemModel.getId());
				transaction.setInventory(storeItemModel.getInventory());
				transaction.setDelivered(storeItemModel.getDelivered());
				transaction.setDelivery(storeItemModel.getDelivery());
				transaction.setLoc(storeItemModel.getLoc());
				transaction.setType(0);
				transaction.setDate(new Date());
				transaction.setAmount(amount);
				TransactionData transactionData = new TransactionData();
				transactionData.insert(transaction);
				
				response.sendRedirect("/admin/store-item/list/"+storeItemModel.getId());
			}
		}
	}
}